package com.pswv2.backstage.more.mywallet.action;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.rules.ErrorCollector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.pswv2.backstage.more.mywallet.service.MyWalletService;
import com.pswv2.bub.bsc.dao.po.AccountDetails;
import com.pswv2.bub.bsc.dao.po.DataManagement;
import com.pswv2.bub.bsc.dao.po.MyWallet;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ErrorCode;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.Paging;

@Controller
@RequestMapping(value = "myWalletAction", produces = { "text/json;charset=UTF-8" })
public class MyWalletAction {
	private static Class<?> poc = MyWallet.class;
	private static Class<?> pocA = AccountDetails.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private MyWalletService myWalletService;
	/**
	 * app支付宝转账
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("transferAccounts")
	public String transferAccounts() {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","2019062665674244","MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCEFV/5h9vT7e+HVX3oJ51DDJXDwcLWiMuEBPJVkdxDi5fgOtIFcV2qMFxNKzcrB+FWIXgJZuuhc82D9cki7IxRSMk3PiRwTjwYyyZj18EwBLcDGa+KxUWfKSUp5VH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kxb4vKRI6/twKBgQCAPx8XsXqniKVAXebcd+5IIeQ5/4ym+q35LwInO5c7+aHejWEnS2VZlZEmpyq9PihJPOQdmy8YRriuZD/QpKTuHen2T0APLXpSx5YkvOsJ41/Odw1ddN5iuTcxFp/zvWKD8Xnt1kr02yTv2m7DPEsaEvm1zNZ3fpJPNpHybk12lwKBgQC5DtXbs2aqpsIqGNSQa/fYhp/sq6bTnz4AHQoEx/B1Wtso+KsAfCDbZhzKaPZJ+IKg7vbTScX4JBrsJlNuGHuS1RaYFt92mREB0L/z3EDJhkk7zXfzWvRCl5gDc7WqD7deJOTYOU8+yO+9M9Q92V1eKN0q8cwum/natJYExy4DfA==","json","UTF-8","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhBVf+Yfb0+3vh1V96CedQwyVw8HC1ojLhATyVZHcQ4uX4DrSBXFdqjBcTSs3KwfhViF4CWbroXPNg/XJIuyMUUjJNz4kcE48GMsmY9fBMAS3AxmvisVFnyklKeVR8iSJ7F8Zzh0vskyXamvf4XTat/IhWr9/Cn9rPR6KCFoUiUkF6kTWjMbJ9YKJ+KDOIcWd0m00/uoShAwvqCYf3vCLmeEYjwDDevKBI09bizuPGxxCSwqESw4nVxqWfqxb8viz0Iy7laaTtNQHTc8F4n2shu7LtGHvD1ZgDmsEzDbeZPdwGVjcduNTHhuC6UZm6xjX0wg7w8UvySHfoUSdIDzzQQIDAQAB","RSA2");
		AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
		request.setBizContent("{" +
		"\"out_biz_no\":\"3142321423432\"," +
		"\"payee_type\":\"ALIPAY_LOGONID\"," +
		"\"payee_account\":\"1965701118@qq.com\"," +
		"\"amount\":\"0.01\"," +
		"\"remark\":\"转账备注\"" +
		"  }");
		AlipayFundTransToaccountTransferResponse response = null;
		try {
			response = alipayClient.execute(request);
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(response.isSuccess()){
		System.out.println("调用成功");
		} else {
		System.out.println("调用失败");
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app支付宝预支付成功
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("prepaidPayment")
	public String prepaidPayment(int userId,double moneyMunber) {
		MyWallet myWallet=myWalletService.queryByUserId(userId);
		double money=0;
		if(myWallet==null){
			MyWallet mw=new MyWallet();
			mw.setMoney(0);
			mw.setUserId(userId);
			extraSpringHibernateTemplate.getHibernateTemplate().save(mw);
			myWallet=myWalletService.queryByUserId(userId);
			return CommonResp.buildErrorResp(BackendErrorCode.not_sufficient_funds).toJsonString();
		}else{
			money=myWallet.getMoney();
			if(money-moneyMunber<0){
				return CommonResp.buildErrorResp(BackendErrorCode.not_sufficient_funds).toJsonString();
			}else{
				double munber = money-moneyMunber;
				myWallet.setMoney(munber);
				extraSpringHibernateTemplate.getHibernateTemplate().update(myWallet);
				//添加账单
				AccountDetails ad=new AccountDetails();
				ad.setUserId(userId);
				ad.setMatter("需求预支付");
				ad.setAmount(moneyMunber);
				ad.setProceedsPaid(0);
				Long s=new Date().getTime();
				ad.setTime(s);
				extraSpringHibernateTemplate.getHibernateTemplate().save(ad);
				List<DataManagement> liDM=extraSpringHibernateTemplate.findAll(DataManagement.class);
				DataManagement dm=liDM.get(0);
				dm.setAllFund(dm.getAllFund()+moneyMunber);
				extraSpringHibernateTemplate.getHibernateTemplate().update(dm);
				AccountDetails adpt=new AccountDetails();
				adpt.setUserId(1);
				adpt.setMatter("客户预支付");
				adpt.setAmount(moneyMunber);
				adpt.setProceedsPaid(0);
				Long s1=new Date().getTime();
				adpt.setTime(s1);
				extraSpringHibernateTemplate.getHibernateTemplate().save(adpt);
				return CommonResp.buildSuccessResp().toJsonString();
			}
		}
	}
	/**
	 * app支付宝支付成功
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("zhifubaoPaySuccess")
	public String zhifubaoPaySuccess(int userId,double moneyMunber) {
		MyWallet myWallet=myWalletService.queryByUserId(userId);
		double money = myWallet.getMoney();
		double munber = money+moneyMunber;
		myWallet.setMoney(munber);
		extraSpringHibernateTemplate.getHibernateTemplate().update(myWallet);
		//添加账单
		AccountDetails ad=new AccountDetails();
		ad.setUserId(userId);
		ad.setMatter("充值");
		ad.setAmount(moneyMunber);
		ad.setProceedsPaid(0);
		Long s=new Date().getTime();
		ad.setTime(s);
		extraSpringHibernateTemplate.getHibernateTemplate().save(ad);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app通过用户Id查询我的钱包详情
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryMyWalletById")
	public String editUserDatum(int userId) {
		List<Criterion> criterions = new ArrayList<>();
		criterions.add(Restrictions.eq("userId", userId));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("myWalletId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

}
